<?php
/**
* @file $Id: Preferences.fnc.php 482 2007-05-02 20:31:35Z focus-sis $
* @package Focus/SIS
* @copyright Copyright (C) 2006 Andrew Schmadeke. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.txt
* Focus/SIS is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.txt for copyright notices and details.
*/

/**
* Returns any user preference
* 
* Each preference has a name ($item) and is in a group ($program).  The 
* preferences set in the Users' Preferences program are in the $program group 
* called "Preferences."  Preferences for teachers' gradebooks are in the $program
* group called "Gradebook."  New Programs requiring preferences can establish 
* new $program groups.
* 
* Default user preferences are hard-coded in the SystemPreferences() function
*
* @param string $item the preference that we'd like returned.
* @param string $program the program which established this preference
* @return string the user-defined preference or else a default value
*/
function Preferences($item,$program='Preferences')
{	global $_FOCUS;

	if(($_SESSION['USERNAME'] || $_SESSION['STUDENT_ID']) && !$_FOCUS['Preferences'][$program])
	{
		$QI=DBQuery("SELECT TITLE,VALUE FROM PROGRAM_USER_CONFIG WHERE USERNAME='".User('USERNAME')."' AND PROGRAM='$program'");
		$_FOCUS['Preferences'][$program] = DBGet($QI,array(),array('TITLE'));

		if(($_SESSION['USERNAME'] || $_SESSION['STUDENT_ID']) && (User('PROFILE')=='parent' || User('PROFILE')=='student'))
		{
			$_FOCUS['Preferences'][$program]['SEARCH'][1]['VALUE'] = 'N';
			$_FOCUS['Preferences'][$program]['MENU'][1]['VALUE'] = 'Top';
		}
	}

	if(!isset($_FOCUS['Preferences'][$program][$item][1]['VALUE']))
		$_FOCUS['Preferences'][$program][$item][1]['VALUE'] = SystemPreferences($item,$program);

	return $_FOCUS['Preferences'][$program][$item][1]['VALUE'];
}

/**
* Returns any system preference
* 
* Each system preference has a name ($item) and is in a group ($program).  The 
* preferences set in the System Preferences program are in the $program group 
* called "system."  Default User Preferences are in the $program
* group called "Preferences."  New programs requiring preferences can establish 
* new $program groups.
* 
* Defautls for system preferences and user preferences are hard-coded here.
*
* @param string $item the preference that we'd like returned.
* @param string $program the program which established this system preference
* @return string the system preference or else a default value
*/
function SystemPreferences($item,$program='system')
{	global $_FOCUS;

	if(!$_FOCUS['SystemPreferences'][$program])
	{
		$QI=DBQuery("SELECT TITLE,VALUE FROM PROGRAM_CONFIG WHERE PROGRAM='$program'");
		$_FOCUS['SystemPreferences'][$program] = DBGet($QI,array(),array('TITLE'));
	}

	$defaults = array(
		'REPORT_CARD'=>'both',
		'SYEAR_TITLE'=>'YYYY-YYYY',
		'VARIABLE_PERIOD_LENGTH'=>'N',
		'CURRENCY'=>'$',
		'DECIMAL'=>'.',
		'WEIGHTS'=>'Y',
		'QUARTERS'=>'Y',
		'WEIGHTED_GPA'=>'Y',
		'DEFAULT_CREDITS'=>'1',
		'BUS_ADDRESSES'=>'Y',
		'MAILING_ADDRESSES'=>'Y',
		'AUTHENTICATION'=>'db',
		'PAST_ATTENDANCE_DAYS'=>0
		);

	$user_defaults = array(
		'SORT'=>'Name',
		'SEARCH'=>'Y',
		'DELIMITER'=>'Tab',
		'COLOR'=>'FFFFCC',
		'HIGHLIGHT'=>'3366FF',
		'THEME'=>'Brushed-Steel',
		'LANGUAGE'=>'en_US',
		'MENU'=>'Side',
		'HIDDEN'=>'Y',
		'WEEKDAY'=>'',
		'MONTH'=>'M',
		'DAY'=>'j',
		'YEAR'=>'Y',
		'MONTH_DAY'=>'mdy',
		'DEFAULT_ALL_SCHOOLS'=>'N',
		'DEFAULT_CUSTOMIZED_LIST'=>'N',
		'DISPLAY_GRADE'=>'Y',
		'DISPLAY_STUDENT_ID'=>'Y',
		'ASSIGNMENT_SORTING'=>'ASSIGNMENT_ID',
		'LABEL_POSITION'=>'bottom',
		'NAME_FORMAT'=>'Y',
		'DEFAULT_TAB'=>3,
		);

	if(User('PROFILE')=='parent' || User('PROFILE')=='student')
		$user_defaults['MENU'] = 'Top';
	if(User('PROFILE')=='teacher')
	{
		$user_defaults['SORT'] = 'Enrolled';
		$user_defaults['SEARCH'] = 'N';
	}

	if(!isset($_FOCUS['SystemPreferences'][$program][$item][1]['VALUE']) && ($program=='Preferences' || $program=='Gradebook'))
		$_FOCUS['SystemPreferences'][$program][$item][1]['VALUE'] = $user_defaults[$item];
	elseif(!isset($_FOCUS['SystemPreferences'][$program][$item][1]['VALUE']))
		$_FOCUS['SystemPreferences'][$program][$item][1]['VALUE'] = $defaults[$item];

	return $_FOCUS['SystemPreferences'][$program][$item][1]['VALUE'];
}
?>